package com.magictower.primary.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.magictower.primary.common.SPConstant;

/**
 * 设置引导页面的基类，因此不用在清单文件中注册
 * Created by kejunzhan on 2017/12/7.
 */

public abstract class BaseSetupActivity extends AppCompatActivity {

    private GestureDetector mDetector;
    public SharedPreferences mPref;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPref = getSharedPreferences(SPConstant.CONFIG, MODE_PRIVATE);

        // 动画效果
        mDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (Math.abs(e2.getRawY() - e1.getRawY()) > 100) {
                    Toast.makeText(BaseSetupActivity.this, "手势划动错误", Toast.LENGTH_SHORT).show();
                    return true;
                }
                if (Math.abs(velocityX) < 100) {
                    Toast.makeText(BaseSetupActivity.this, "划动速度太慢哦", Toast.LENGTH_SHORT).show();
                    return true;
                }
                //向右划 上一页
                if (e2.getRawX() - e1.getRawX() > 150) {
                    showPreviousPage();
                    return true;
                }
                //向左划 下一页
                if (e1.getRawX() - e2.getRawX() > 150) {
                    showNextPage();
                    return true;
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    public void previous(View view) {
        showPreviousPage();
    }

    public void next(View view) {
        showNextPage();
    }

    public abstract void showNextPage();

    public abstract void showPreviousPage();

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);//触摸事件
        return super.onTouchEvent(event);
    }
}
